package com.meiborte.www;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Vector;

public class GameFrame extends JFrame {

    HeroPlane heroPlane;

    //定义子弹的集合
    Vector<Bullet>bullets=new Vector<>();
    //敌机集合
    Vector<EnemyPlane>enemys=new Vector<>();
    GameFrame frame;
    public GameFrame() {
        frame=this;
        //创建飞机
        heroPlane = new HeroPlane();
        heroPlane.start();
        // 设置窗口大小
        this.setSize(500, 760);
        // 标题
        this.setTitle("雷霆战机");
        this.setResizable(false);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        // 使窗口可见
        this.setVisible(true);

        // 启动一个线程用于重新绘制窗口
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    repaint();
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        //产生敌机的线程
        new Thread(new Runnable() {
            //创建随机数的对象
            Random r =new Random();

            @Override
            public void run() {
                while (true){
                    EnemyPlane enemyPlane=new EnemyPlane(r.nextInt(500),0,frame);
                    enemyPlane.start();
                    //添加敌机的时候，让x轴随机
                    enemys.add(enemyPlane);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    /**
     * 在窗口上画，内容，paint这个画笔的方法窗口初始化的时候会默认的执行
     * @param g
     */
    public void paint(Graphics g) {
        BufferedImage image =(BufferedImage) this.createImage(this.getSize().width, this.getSize().height);
        Graphics bi = image.getGraphics();

        // 绘制背景
        bi.drawImage(new ImageIcon("img/beijing.png").getImage(),0,0,null);
        //画战斗机
        bi.drawImage(heroPlane.img,heroPlane.x,heroPlane.y,heroPlane.width,heroPlane.height,null);
        // 飞机发射子弹

        for (int i = 0; i < bullets.size(); i++) {
            System.out.println(bullets);
            Bullet bullet=bullets.get(i);
            if(bullet.y>0)
                 bi.drawImage(bullet.image,bullet.x,bullet.y-=bullet.speed,bullet.width,bullet.height,null);
            else
                bullets.remove(bullet);
        }
        //画敌机
        for (int i = 0; i < enemys.size(); i++) {
            System.out.println(bullets);
            EnemyPlane ep=enemys.get(i);
            if(ep.y<760)
                bi.drawImage(ep.img,ep.x,ep.y+=ep.speed,ep.width,ep.height,null);
            else
                enemys.remove(ep);
        }
        // 生效
        g.drawImage(image, 0, 0, null);
    }

    public static void main(String[] args) {
        GameFrame frame=new GameFrame();
        Player player=new Player(frame);
        frame.addKeyListener(player);
    }

}
